package com.pucong.gitee.database.backup.business.service.controller;

import com.pucong.gitee.database.backup.business.service.service.ServiceFileService;
import com.pucong.gitee.database.backup.common.entity.service.ServiceFile;
import com.pucong.gitee.database.backup.common.entity.service.dto.*;
import com.pucong.gitee.database.backup.common.entity.service.vo.ServiceFileTreeVo;
import com.pucong.gitee.database.backup.utils.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/auto/fileServer")
@Api(tags = "9. 文件服务维护 API")
public class ServiceFileController {

    @Autowired
    private ServiceFileService serviceFileService;

    @GetMapping("/getFileServerById/{uuid}")
    @ApiOperation(value = "01根据id查询")
    public Res<ServiceFile> getFileServerById(@ApiParam("文件服务id") @PathVariable("uuid") String uuid) {
        ServiceFile serviceFile = serviceFileService.getFileServerById(uuid);
        return Res.ok(serviceFile);
    }

    @PostMapping("/insertFileServer")
    @ApiOperation(value = "02新增")
    public Res<Integer> insertFileServer(@Valid @RequestBody ServiceFileSaveDto dto) {
        return Res.ok(serviceFileService.insertFileServer(dto));
    }

    @PutMapping("/updateFileServerById/{uuid}")
    @ApiOperation(value = "03根据id更新数据")
    public Res<Integer> updateFileServerById(@ApiParam("文件服务id") @PathVariable("uuid") String uuid, @RequestBody ServiceFileSaveDto dto) {
        return Res.ok(serviceFileService.updateFileServerById(uuid, dto));
    }

    @GetMapping("/getFileServerCatalogueById/{id}")
    @ApiOperation(value = "04根据id和目录层级查询下级文件")
    public Res<List<ServiceFileTreeVo>> getFileServerCatalogueById(@ApiParam("文件服务id") @PathVariable("id") String id, @ApiParam("目录层级") String cataloguePath) {
        List<ServiceFileTreeVo> serviceFileTreeVos = serviceFileService.getFileServerCatalogueById(id, cataloguePath);
        return Res.ok(serviceFileTreeVos);
    }

    @PostMapping("/uploadFile/{uuid}")
    @ApiOperation(value = "05文件上传")
    public Res<Boolean> uploadFile(@ApiParam("文件服务id") @PathVariable("uuid") String uuid, @RequestBody ServiceFileUploadDto dto) {
        Boolean aBoolean = serviceFileService.uploadFile(uuid, dto);
        return Res.ok(aBoolean);
    }

    @PostMapping("/uploadFileList/{uuid}")
    @ApiOperation(value = "文件list上传")
    public Res<Boolean> uploadFile(@ApiParam("文件服务id") @PathVariable("uuid") String uuid, @RequestBody ServiceFilesUploadDto dto) {
        Boolean aBoolean = serviceFileService.uploadFileList(uuid, dto);
        return Res.ok(aBoolean);
    }

    @DeleteMapping("/deleteFile/{uuid}")
    @ApiOperation(value = "06文件删除")
    public Res<Boolean> deleteFile(@ApiParam("文件服务id") @PathVariable("uuid") String uuid, ServiceFileDeleteDirDto dto) {
        Boolean aBoolean = serviceFileService.deleteFile(uuid, dto.getFilePath());
        return Res.ok(aBoolean);
    }

    @DeleteMapping("/deleteDir/{uuid}")
    @ApiOperation(value = "目录删除")
    public Res<Boolean> deleteDir(@ApiParam("文件服务id") @PathVariable("uuid") String uuid, ServiceFileDeleteDirDto dto) {
        Boolean aBoolean = serviceFileService.deleteDir(uuid, dto.getFilePath());
        return Res.ok(aBoolean);
    }

    @GetMapping("/downLoadFileByPath/{uuid}")
    @ApiOperation(value = "07下载文件")
    public void downLoadFileByPath(@ApiParam("文件服务id") @PathVariable("uuid") String uuid, @ApiParam("路径") String path, HttpServletResponse response) {
        serviceFileService.downLoadFileByPath(uuid, path, response);
    }

    @GetMapping("/lookFileByPath/{uuid}")
    @ApiOperation(value = "08预览pdf、图片文件")
    public void lookFileByPath(@ApiParam("文件服务id") @PathVariable("uuid") String uuid, @ApiParam("路径") String path, HttpServletResponse response) {
        serviceFileService.lookFileByPath(uuid, path, response);
    }

    @PostMapping("/connectTest")
    @ApiOperation(value = "连接测试")
    public Res<Boolean> connectTest(@Validated @RequestBody ServiceFileConnectTestDto dto) {
        Boolean i = serviceFileService.connectTest(dto);
        return Res.ok(i);
    }

    @PostMapping("/addCatalogue/{uuid}")
    @ApiOperation(value = "创建空文件")
    public Res<Boolean> addCatalogue(@ApiParam("文件服务id") @PathVariable("uuid") String uuid,@Validated @RequestBody ServiceFileAddDirDto dto) {
        Boolean i = serviceFileService.addCatalogue(uuid, dto);
        return Res.ok(i);
    }

    @PostMapping("/rename/{uuid}")
    @ApiOperation(value = "重命名")
    public Res<Boolean> rename(@ApiParam("文件服务id") @PathVariable("uuid") String uuid,@Validated @RequestBody ServiceFileRenameDto dto) {
        Boolean i = serviceFileService.rename(uuid, dto);
        return Res.ok(i);
    }
}
